package com.duowei.diandan.presenter;

import com.duowei.diandan.app.OrderList;
import com.duowei.diandan.bean.food.OrderItem;
import com.duowei.diandan.db.entry.Wmlsb;
import com.duowei.diandan.event.ToastEvent;
import com.duowei.diandan.fragment.OrderFragment;
import com.duowei.diandan.fragment.dialog.InputDialogFragment;
import com.duowei.diandan.fragment.dialog.TasteDialogFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class OrderItemPresenter {

    private OrderFragment mFragment;

    public OrderItemPresenter(OrderFragment fragment) {
        mFragment = fragment;
    }

    // 点击 -
    public void onRemoveButtonClick(OrderItem item) {
        if (canEditable(item.getMain())) {
            OrderList.getInstance().removeCartItem(item, list -> {
                EventBus.getDefault().post(new ToastEvent(item.getMain().getXmmc() + " -1"));
                mFragment.refreshDataSync();
            });
        } else {
            EventBus.getDefault().post(new ToastEvent("不能修改", false));
        }

    }

    // 点击 +
    public void onAddButtonClick(OrderItem item) {
        if (canEditable(item.getMain())) {
            OrderList.getInstance().addCartItem(item, list -> {
                EventBus.getDefault().post(new ToastEvent(item.getMain().getXmmc() + " +1"));
                mFragment.refreshDataSync();
            });
        } else {
            EventBus.getDefault().post(new ToastEvent("不能修改", false));
        }
    }

    // 点击口味
    public void onTasteClick(OrderItem item) {
        if (canEditable(item.getMain())) {
            TasteDialogFragment fragment = TasteDialogFragment.newInstance(item.getMain().getId());
            //noinspection ConstantConditions
            fragment.show(mFragment.getFragmentManager(), null);
            fragment.setOnClickListener(() -> {
                mFragment.refreshDataSync();
            });

        } else {
            EventBus.getDefault().post(new ToastEvent("不能修改", false));
        }
    }

    //点击数量
    public void onChangeNum(OrderItem item) {
        if (canEditable(item.getMain())) {
            InputDialogFragment fragment = InputDialogFragment
                    .newInstance("请输入数量", "", true);
            fragment.show(mFragment.getFragmentManager(), null);
            fragment.setOkBtnClickListener(text -> {
                float inputMoney = Float.valueOf(text);
                if (inputMoney <= 0) {
                    EventBus.getDefault().post(new ToastEvent("数量需大于0", false));
                    return;
                }
                OrderList.getInstance().changeCartItem(item, inputMoney, list -> {
                    EventBus.getDefault().post(new ToastEvent(item.getMain().getXmmc() + text));
                    mFragment.refreshDataSync();
                });
            });
        } else {
            EventBus.getDefault().post(new ToastEvent("不能修改", false));
        }
    }

    private boolean canEditable(Wmlsb wmlsb) {
        return wmlsb.getXh() == null && !"1".equals(wmlsb.getSfyxd());
    }
}
